package UserInterfase;

import core.IvoCheckers;
import core.PlayNetGame;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StartGame extends javax.swing.JDialog {

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public void initComponents(int posX, int posY) {

        StartButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        MainLabel = new javax.swing.JLabel();
        setLocation(posX, posY);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        
        StartButton.setText("Start New Game");
        StartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        MainLabel.setText(BeginString);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(StartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(MainLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(99, 99, 99))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StartButton)
                    .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
        );

        pack();
        this.setTitle("Start New Game");
        this.setModal(true);
        this.setVisible(true);
    }// </editor-fold>

    private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {
        newConnect = new AcceptConnection();
        newConnect.start();
        StartButton.setEnabled(false);
        MainLabel.setText(WaitString);
    }

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        if (newConnect.isAlive())
            newConnect.stopThread();
        StartButton.setEnabled(true);
        MainLabel.setText(BeginString);
    }
    private void formWindowClosed(java.awt.event.WindowEvent evt) {
        if (newConnect.isAlive())
        {
            newConnect.stopThread();
        }
    }
    
    public static void BeginGame(boolean isAcceptConnection)
    {
            IvoCheckers.startNewGame();
            IvoCheckers.getGame().setPlayerTurn(false);
            SimpleGUI.windowStartGame.setVisible(false);
            SimpleGUI.windowStartGame.dispose();
            PlayNetGame netGame = new PlayNetGame();
            netGame.start();
    }
    // Variables declaration - do not modify

    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel MainLabel;
    private javax.swing.JButton StartButton;
    AcceptConnection newConnect;
    private String BeginString = "Start New Game and wait for other player!";
    private String WaitString = "WAIT...";
    // End of variables declaration
}

class AcceptConnection extends Thread
{
    public ServerSocket serverSocket;
    public boolean isEnd = false;
    public void run()
    {
        boolean result;
        try {
            serverSocket = new ServerSocket(5555);
            result = IvoCheckers.startNewNetGame(serverSocket);
            if (result)
            {
                SimpleBoard.setIsNetGame(true);
                SimpleBoard.setIsLightColor(true);
                StartGame.BeginGame(result);
                
            }
        } catch (IOException ex) {
           ex.printStackTrace();

        }
        isEnd = true;
    }
    public void stopThread()
    {
        try {
            serverSocket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        while (!isEnd)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                return;
            }
        }
    }
}
